package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.eventbus.NumberOfArticleStates;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.LoginUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.userInfo.adapter.pageadapter.PublishAuditDraftPagerAdapter;
import com.rongshu.view.userInfo.entity.CollectedWorkStatusCount;
import com.rongshu.view.userInfo.iview.CollectedWorkListView;
import com.rongshu.view.userInfo.presenter.CollectedWorkListPresenter;
import com.rongshu.widget.TabPageIndicator;
import com.umeng.socialize.utils.CommonUtil;
import com.widget.NoScrollViewPager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.rongshu.utils.UserInfoUtils.getUserId;

public class CollectedWorkListAcitivity extends BaseActivity<CollectedWorkListPresenter> implements CollectedWorkListView {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;//标题
    @BindView(R.id.indicator)
    TabPageIndicator indicator;
    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;
    //intent
    private Intent intent = null;
    //文集id
    private int serialize = 0;
    //文集名称
    private String articleName = "";
    //title
    private List<String> titles = null;
    //index
    private int index = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_collected_works_list_view;
    }

    @Override
    protected void initView() {
        setPresenter(new CollectedWorkListPresenter(this), this);
        //intent
        intent = getIntent();
        if (intent != null) {
            serialize = intent.getIntExtra(Constant.INTENT_MESSAGE, 0);
            articleName = intent.getStringExtra(Constant.INTENT_TITLE);
        }
        //title
        txt_top_title.setText(articleName);
        titles = new ArrayList<>();
        titles.add("已发表(0)");
        titles.add("审核中(0)");
        titles.add("草稿箱(0)");
        //viewpager
        PublishAuditDraftPagerAdapter pageAdapter = new PublishAuditDraftPagerAdapter(getSupportFragmentManager());
        pageAdapter.setSerialize(serialize);
        pageAdapter.setTitles(titles);
        viewPager.setAdapter(pageAdapter);
        viewPager.setNoScroll(true);
        indicator.setViewPager(viewPager);
        indicator.setIndicatorMode(TabPageIndicator.IndicatorMode.MODE_WEIGHT_NOEXPAND_SAME);// 设置模式，一定要先设置模式
        indicator.setTextColor(ContextCompat.getColor(this, R.color.color_2c2c2c));
        indicator.setIndicatorColor(ContextCompat.getColor(this, R.color.color_3CC051));// 设置底部导航线的颜色
        indicator.setTextColorSelected(ContextCompat.getColor(this, R.color.color_3CC051));// 设置tab标题选中的颜色
        indicator.setTextSize((int) getResources().getDimension(R.dimen.dd_dimen_28px));// 设置字体大小
        viewPager.setCurrentItem(index);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getStatusCount();
    }

    /**
     * 获取我的文集里不同文章状态的数量
     */
    public void getStatusCount() {
        //user_id
        int user_id = UserInfoUtils.getUserId();
        if (user_id == 0) {
            LoginUtils.toLogin(this);
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userId", user_id);
        map.put("serializeId", serialize);
        presenter.getStatusCount((int) map.get("userId"),
                (int) map.get("serializeId"),
                Common.encryptionSign(map));
    }

    @OnClick({R.id.layout_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
        }
    }


    @Override
    public void onStatusCount(CollectedWorkStatusCount statusCount) {
        if (statusCount.getErrCode() == 0) {
            CollectedWorkStatusCount.ResponseBean bean = statusCount.getResponse();
            if (bean != null) {
                String published = "已发表(" + bean.getPublished() + ")";
                String inReview = "审核中(" + bean.getInReview() + ")";
                String draft = "草稿箱(" + bean.getDraft() + ")";
                indicator.setStatusCountList(Arrays.asList(published, inReview, draft));
            }
        }
    }

   @Subscribe
   public void onNumberOfArticleStates(NumberOfArticleStates states){
        getStatusCount();
   }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
